import { Component, ViewChild, Input } from '@angular/core';
import { NavParams, Events, AlertController, App, ModalController, NavController, Content, ActionSheetController, LoadingController } from 'ionic-angular';
import { Storage } from '@ionic/storage';
import { Config } from '../../../../../providers/config';
import { DomSanitizer } from '@angular/platform-browser';
import _ from 'lodash';
import * as moment from "moment";
import { IrpInternalReportDetailPage } from '../detail/detail.component';
import { Common } from "../../../../../providers/common";
import { IrpInternalReportListService } from "../../inside/list/list.service"
import { from } from 'rxjs/observable/from';

@Component({
  selector: 'page-irp-report-inside-list',
  templateUrl: './list.html',
  providers: [IrpInternalReportListService]
})
export class IrpInternalReportListPage {
  moa_page_name: any = "page-irp-report-inside-list";
  moa_page_explain: any = "内部研报";

  @Input() public isFromMainPage: boolean;

  // 列表数据
  listData: any = [];
  // 页码
  pageIndex = 1;
  public minnum:any;
  public maxnum:any;
  isLoadMore = true;

  constructor(
    public alertCtrl: AlertController,
    public app: App,
    public modalCtrl: ModalController,
    public navCtrl: NavController,
    public config: Config,
    private sanitizer: DomSanitizer,
    public events: Events,
    public storage: Storage,
    private common: Common,
    private actionSheetCtrl: ActionSheetController,
    private navParams: NavParams,
    public loadingCtrl: LoadingController,
    public listService: IrpInternalReportListService
  ) {
    this.minnum = 1;
    this.maxnum = 10;
    this.loadListData(1,10,null, null)
  }

  loadListData(minnum,maxnum,infiniteScroll, refresher) {
    let that = this;
    this.listService.getIRMReport(minnum,maxnum).subscribe((data) => {
      if (data) {
        // 如果加载第一页，则清空数据
        //  if(maxnum = 10){
        //    that.isLoadMore = true;
        //    that.listData = [];
        //  }
        // 把现有数据和新加载的数据合并
        this.listData = _.concat(this.listData, data);
        // 增加搜索索引
        this.pageIndex = this.pageIndex + 1;

        // 如果返回数据少于请求页数，则不再加载更多
        if (data.length < 10) {
          this.isLoadMore = false;
        }
      } else {
        this.listData = [];
        this.isLoadMore = false;
      }
      // complete event
      if (infiniteScroll) {
        infiniteScroll.complete();
      }
      if (refresher) {
        refresher.complete();
      }
    })
  }
  getListData(item) {
    this.navCtrl.push(IrpInternalReportDetailPage, item);
  }
  doRefresh(refresher) {
    let that = this;
    this.minnum = 1;
    this.maxnum = 10;
    this.isLoadMore = true;
    this.loadListData(this.minnum,this.maxnum,null, refresher);
    setTimeout(() => {
      refresher.complete();
    }, 1000);

  }

  doInfinite(infiniteScroll) {
    if (this.pageIndex != 1) {
      let that = this;
      this.minnum = that.minnum + 10;
      this.maxnum = that.maxnum + 10;
      console.info("-----------infiniteScroll------------");
      console.info(infiniteScroll);
      console.log(this.listData);
      if (this.isLoadMore) {
        this.loadListData(this.minnum,this.maxnum,infiniteScroll, null);
      } else {
        infiniteScroll.complete();
      }
    } else {
      infiniteScroll.complete();
    }
  }
  getTimes(){
    document.getElementById("time").nodeValue = moment().format('LL');

  }

}